# -*- coding: utf-8 -*-
import os
from MsgProcess import MsgProcess, MsgType
import time
#导入OpenCV库
import cv2
#HyperLPR 框架 

class AiCar(MsgProcess):

    def Start(self,x):
        global hyperlpr
        os.system("sudo pip3 install hyperlpr==0.0.1 -i https://pypi.tuna.tsinghua.edu.cn/simple/")
        try:
            import hyperlpr as hyperlpr
        except:
            pass

    def Text(self, message):

        try:
        #     name=""
        #     #窗口名称
        #     cv2.namedWindow(name, cv2.WINDOW_NORMAL )
        #     #窗口最大化
        #     cv2.setWindowProperty(name , cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
        #     #摄像头
        #     cap = cv2.VideoCapture(0)
        #     nowtime=time.time()
        #     while cap.isOpened():
        #         #读取一帧数据
        #         ok, frame = cap.read()
        #         if not ok:            
        #             break 
        #         #显示方向
        #         frame = cv2.flip(frame,1)

        #         #显示图像
        #         cv2.imshow(name, frame)
        #         c = cv2.waitKey(10)
        #         #键盘q退出视频
        #         if c & 0xFF == ord('q') or time.time()-nowtime>1.5:
        #             break
        #     #===============================
        #    # cv2.imwrite("plugin/AiCar/demo.jpg" ,frame) 
        #     #释放摄像头并销毁所有窗口
        #     cap.release()
        #     cv2.destroyAllWindows()

        
            #树莓派的排线摄像头原始照片质量极高
            os.system("sudo raspistill  -t 500 -o plugin/AiCar/demo.jpg")
            image = cv2.imread("plugin/AiCar/demo.jpg")
            data = hyperlpr.HyperLPR_PlateRecogntion(image)
            if data:
                for x in data:
                    self.say(  x[0] )
            else:
                self.say("未识别")

        except:
            self.say("本插件依赖未安装完成，重启设备后，稍后尝试!")


        

